const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getPhoneNumber(e, that, sKey) { //点击获取手机号码按钮
  console.log('手机号===>', e)
  const ency = e.detail.encryptedData;
  const iv = e.detail.iv;

  if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //不同意
    that.setData({
      modalstatus: true
    });

  } else { //同意授权

    wx.request({
      method: "POST",
      url: getApp().Api_url + 'user/getUserPhone',
      data: {
        encryptedData: ency,
        iv: iv,
        sessionData: sKey
      },
      header: {
        //'content-type': 'application/json' // 默认值
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: (res2) => {

        console.log("解密成功~~~~~~~将解密的号码保存到本地~~~~~~~~");

        console.log('res2===>', res2);
        if (res2.data.code == 200) {
          
          var phone = res2.data.data;
          console.log('phone===>', phone);
          wx.setStorageSync('phone', phone);
          that.setData({
            isGetPhone: false,
            steps: 2,
            state: 1

          })
        } else {
          wx.showToast({
            title: '获取手机号失败,请您重新授权',
            icon: 'none'
          })
        }

      },
      fail: function (res) {

        console.log("解密失败~~~~~~~~~~~~~");

        console.log(res);
        wx.showToast({
          title: '获取手机号失败,请您重新授权',
          icon: 'none'
        })

      }

    });

  }

}

function toThousands(num) {
  return (num || "").replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}

/* 图片缩略
-------------------------------------- */
function imgThumbnail(img, height = 200, widht = 200) {
  return img.replace(/files/g, 'static') + `!c${height}x${widht}.jpg`
}
module.exports = {
  formatTime: formatTime,
  _getPhoneNumber: getPhoneNumber,
  toThousands,
  imgThumbnail
}